package com.seckill.Cache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Set;

/**
 * 热点缓存
 * @author 不鄢
 */
@Component
public class HotCache {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public boolean addHotKey(String keyType,String keyId){
        //使用Redis中的ZSet（有序集合）
        return redisTemplate.opsForZSet().addIfAbsent(keyType,keyId,0);
    }

    public void addHotScore(String keyType,String keyId,Integer delta){
        redisTemplate.opsForZSet().addIfAbsent(keyType,keyId,delta);
    }

    /**
     * 根据热点返回所有热点key
     * @param keyType
     * @return
     */
    public Set<String> getByHotScore(String keyType){
        return redisTemplate.opsForZSet().reverseRange(keyType,0,Integer.MAX_VALUE);
    }
}
